package com.google.code.p.slideunlocker;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

import com.google.code.p.slideunlocker.io.IOUtils;

public class TermsConditions extends Activity{

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        // Close the menu
        if (Intent.ACTION_MAIN.equals(intent.getAction())) {
            getWindow().closeAllPanels();
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        stopService(new Intent(this, SleepListenService.class));
        final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        boolean terms = settings.getBoolean("terms", true);
        if(!terms){
            startActivity(new Intent(this, SlideUnlocker.class));
            finish();
        }else{
            setContentView(R.layout.terms);
            Button yes = (Button) findViewById(R.id.yes);
            Button no = (Button) findViewById(R.id.no);
            
            WebView wv = (WebView) findViewById(R.id.gpl3);
            try {
                wv.loadData(IOUtils.toString(getResources().openRawResource(R.raw.gpl)).toString(), "text/html", "utf-8");
            } catch (Exception e) {
                throw new RuntimeException("Blowup");
            }
            
            showMessage("This project is under GPL3, you must agree to the terms and conditions before proceeding");
            
            
            yes.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(TermsConditions.this, SlideUnlocker.class));
                    Editor editor = settings.edit();
                    editor.putBoolean("terms", false);
                    editor.commit();
                    finish();
                }
            });
            
            no.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            yes.setVisibility(View.VISIBLE);
            no.setVisibility(View.VISIBLE);
        }

    }
    
    
    private void showMessage(String errorString) {
      Toast text = Toast.makeText(this, 
              errorString, Toast.LENGTH_LONG);
      text.show();
    }
}